var UIState = { INITIAL : 0, MOVING: 1, ATTACKING: 2, MOVE: 3, ATTACK: 4};

function StateMachine()
{
    this.reset = function()
    {
        this.state = UIState.INITIAL;
        this.from = undefined;
        this.to = undefined;
    }
    
    this.changeState = function(game, player, x, y)
    {
        if(game.currentPlayer() != player) return;
        if(x >= game.info.width || y >= game.info.height) return;
        
        var hierarkia = game.hierarkia();
        var side = game.currentSide();
        
        cell = hierarkia.get(x, y);
        if(this.state == UIState.INITIAL)
        {
            if(cell && cell.side == side && hierarkia.canMove(x, y))
            {
                this.state = UIState.MOVING;
                this.from = [x,y];
            } else
                this.reset();

            return true;
        } else if(this.state == UIState.MOVING)
        {
            var from = this.from;

            if(hierarkia.canMoveTo(from[0], from[1], x, y))
            {
                this.to = [x,y];
                this.state = UIState.MOVE;
            } else if(hierarkia.canAttackTo(from[0], from[1], x, y))
			{
                this.to = [x,y];
                this.state = UIState.ATTACK;
			} else if(cell && cell.side == side && hierarkia.canMove(x, y))
            {
                this.from = [x,y];
            } else
                this.reset();

            return true;                
        }
        
        return false;
    }
    
    this.reset();
}

